package com.yk.airline.process;

import com.yk.airline.component.ExcelExport.ExcelExportUtil;
import com.yk.airline.component.analysisdata.AnalysisDataUtil;
import com.yk.airline.component.autoemails.api.IMailSender;
import com.yk.airline.component.crawler.api.ICrawlerService;
import com.yk.airline.dao.airline.api.IAirLineDao;
import com.yk.airline.dao.capturemark.api.ICaptureMarkDao;
import com.yk.airline.dao.capturewebsite.api.ICaptureWebsiteDao;
import com.yk.airline.dao.city.api.ICityDao;
import com.yk.airline.model.AirLineModel;
import com.yk.airline.model.CaptureMarkModel;
import com.yk.airline.model.CaptureWebsiteModel;
import com.yk.exportExcel.entity.Template;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * Project Name:airlinecrawler
 * File Name:Main
 * Package Name:com.yk.airline.process
 * Date:2018/2/7 10:14
 * Author:zhangshaoyang
 * Description:
 * Copyright (c) 2018, 重庆云凯科技有限公司 All Rights Reserved.
 */
@EnableScheduling
@Component
public class Main {
    @Resource(name = "crawlerService")
    private ICrawlerService crawlerService;
    @Resource(name = "airLineDao")
    private IAirLineDao airLineDao;
    @Resource(name = "captureMarkDao")
    private ICaptureMarkDao captureMarkDao;
    @Resource(name = "captureWebsiteDao")
    private ICaptureWebsiteDao captureWebsiteDao;
    @Resource(name = "analysisDataUtil")
    private AnalysisDataUtil analysisDataUtil;
    @Resource(name = "excelExportUtil")
    private ExcelExportUtil excelExportUtil;
    @Resource(name = "mailSenderImpl")
    private IMailSender mailSender;

    @Scheduled(cron = "0 2/10 * * * ? ")
    public void process() throws FileNotFoundException, ParseException, InterruptedException {
        //1.设定抓取日期
        String currentDay = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());
        //2.初始化批次号
        CaptureMarkModel captureMarkModel = new CaptureMarkModel();
        captureMarkModel.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        captureMarkModel.setCreateTime(new Timestamp(System.currentTimeMillis()));
        captureMarkModel.setCaptureTime(new Timestamp(System.currentTimeMillis()));
        this.captureMarkDao.save(captureMarkModel);
        //3.指定航线
        List<AirLineModel> airLineModels = airLineDao.findAll();
//                .stream()
//                .filter(airLineModel -> airLineModel.getCityByArrivePlace().getName().equals("深圳"))
//                .collect(Collectors.toList());
        //4.指定抓取网站
        List<CaptureWebsiteModel> captureWebsiteModels = captureWebsiteDao.findAll();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 12, 30000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>());
        //线程数
        final CountDownLatch count = new CountDownLatch(airLineModels.size() * captureWebsiteModels.size() * getDateAfter(currentDay).length);
        for (CaptureWebsiteModel captureWebsiteModel : captureWebsiteModels) {
            System.out.println("抓取网站名称：" + captureWebsiteModel.getName());
            for (AirLineModel airLineModel : airLineModels) {
                for (String time : getDateAfter(currentDay)) {
                    executor.execute(() -> {
                        System.setProperty("webdriver.gecko.driver", "C:\\WorkEnvir\\geckodriver\\geckodriver.exe");
                        FirefoxDriver webDriver = new FirefoxDriver();
                        System.out.println("航线：" + airLineModel.getCityByOriginPlace().getName() + "-" + airLineModel.getCityByArrivePlace().getName());
                        //5.抓取并存入数据库
                        try {
                            this.crawlerService.crawlerHtmlByWebdriver(webDriver,captureMarkModel, airLineModel, captureWebsiteModel, time);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            count.countDown();
                        }
                    });
                }
            }
        }
        count.await(15 * 60, TimeUnit.SECONDS);

        //6.解析数据
        this.analysisDataUtil.startRecentDataAnalysis();
        //7.导出excel
        List<String> companies = new ArrayList<>();
        companies.add("CA");
        companies.add("3U");
        companies.add("MU");
        this.excelExportUtil.run(companies, Template.TemplateOne, "C://excel");
        List<String> fileList = this.excelExportUtil.run(companies, Template.TemplateTwo, "C://excel");
        fileList.stream().forEach(item -> {
            System.out.println("the file path is:===========>" + item);
        });
        //8.发送Email
        String title = "国航数据采集-" + captureMarkDao.findTheLatestMark().getCaptureTime();
        String context = "国航数据采集：\n开始抓取时间：" + captureMarkDao.findTheLatestMark().getCaptureTime();
        this.mailSender.sendMail(title, context, fileList);
    }


    private static String[] getDateAfter(String specifiedDay) throws ParseException {
        int days[] = {0, 2, 4, 10};
        String dayAfters[] = new String[4];
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        for (int i = 0; i < 4; i++) {
            now.set(Calendar.DATE, now.get(Calendar.DATE) + days[i]);
            String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(now.getTime());
            dayAfters[i] = dayAfter;
            System.out.println(dayAfters[i]);
        }
        return dayAfters;
    }
}
